package network

import (
	"fmt"
	eipv2 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2"
	"github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2/model"
	regionv2 "github.com/huaweicloud/huaweicloud-sdk-go-v3/services/eip/v2/region"
)

type EIPGreeter interface {
	EIP(region string) EIPInterface
}

// EIPInterface 弹性公网IP接口
type EIPInterface interface {
}

type eip struct {
	network *Network
	region  string
	client  *eipv2.EipClient
}

func newEIP(network *Network, region string) *eip {
	_client := eipv2.NewEipClient(
		eipv2.EipClientBuilder().
			WithRegion(regionv2.ValueOf(region)).
			WithCredential(network.credentials).
			Build())
	return &eip{
		region:  region,
		network: network,
		client:  _client,
	}
}

// CreateEIPInstance 创建弹性公网IP
func (e *eip) CreateEIPInstance(name string, sizeBandwidth int32) {
	request := &model.CreatePublicipRequest{}
	ipVersion := model.GetCreatePublicipOptionIpVersionEnum().E_4
	ipbody := &model.CreatePublicipOption{
		Type:      "5_sbgp",
		IpVersion: &ipVersion,
	}
	chargeModeBandwidth := model.GetCreatePublicipBandwidthOptionChargeModeEnum().TRAFFIC
	body := &model.CreatePublicipBandwidthOption{
		ChargeMode: &chargeModeBandwidth,
		Name:       &name,
		ShareType:  model.GetCreatePublicipBandwidthOptionShareTypeEnum().PER,
		Size:       &sizeBandwidth,
	}
	request.Body = &model.CreatePublicipRequestBody{
		Publicip:  ipbody,
		Bandwidth: body,
	}
	response, err := e.client.CreatePublicip(request)
	if err == nil {
		fmt.Printf("%+v\n", response)
	} else {
		fmt.Println(err)
	}
}

// DeleteEIPInstance 删除弹性公网IP
func (e *eip) DeleteEIPInstance(id string) bool {
	request := &model.DeletePublicipRequest{}
	request.PublicipId = id
	response, err := e.client.DeletePublicip(request)
	if err != nil {
		fmt.Println(response)
		return false
	}
	return true
}
